package org.vs.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.vs.config.StaticResourceConfig;
import org.vs.result.RestResult;
import org.vs.result.RestResultBuilder;
import org.vs.service.StaticResourceService;
import org.vs.util.StringUtil;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Slf4j
@Service
public class StaticResourceServiceImpl implements StaticResourceService {
    @Resource
    private StaticResourceConfig staticResourceConfig;

    @Override
    public RestResult upload(MultipartFile file) {
        if (file == null) {
            return new RestResultBuilder().fail("数据为空");
        }

        String fileName = UUID.randomUUID() + ".png";

        FTPClient ftpClient = new FTPClient();

        try {
            ftpClient.connect(staticResourceConfig.getIp(), 21);
            ftpClient.login(staticResourceConfig.getUsername(),
                    staticResourceConfig.getPassword());

            int replyCode = ftpClient.getReplyCode();

            if (!FTPReply.isPositiveCompletion(replyCode)) {
                log.info("连接 ftp 服务器失败");
                return new RestResultBuilder().fail("连接 ftp 服务器失败");
            }

            InputStream inputStream = file.getInputStream();
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

            boolean result = ftpClient.storeFile(fileName, inputStream);

            if (!result) {
                log.info("上传文件失败");
                return new RestResultBuilder().fail("上传文件失败");
            }

        } catch (Exception e) {
            log.info("上传文件失败 {}", e);
            return new RestResultBuilder().fail("上传文件失败");
        } finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return new RestResultBuilder().success((Object)
                (staticResourceConfig.getUrlPrefix() +
                        StringUtil.getFilePathInServer(staticResourceConfig.getPath(), fileName)));
    }

    @Override
    public RestResult delete(String file) {
        if (file == null) {
            return new RestResultBuilder().fail("数据为空");
        }

        FTPClient ftpClient = new FTPClient();

        try {
            ftpClient.connect(staticResourceConfig.getIp(), 21);
            ftpClient.login(staticResourceConfig.getUsername(),
                    staticResourceConfig.getPassword());

            int replyCode = ftpClient.getReplyCode();

            if (!FTPReply.isPositiveCompletion(replyCode)) {
                log.info("连接 ftp 服务器失败");
                return new RestResultBuilder().fail("连接 ftp 服务器失败");
            }

            boolean result = ftpClient.deleteFile(file.substring(file.lastIndexOf("/")));

            if (!result) {
                log.info("删除文件失败");
                return new RestResultBuilder().fail("删除文件失败");
            }
        } catch (Exception e) {
            log.info("删除文件失败 {}", e);
            return new RestResultBuilder().fail("删除文件失败");
        } finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return new RestResultBuilder().success();
    }
}
